//
//  JTAlertView.m
//  textext
//
//  Created by rimi on 16/7/12.
//  Copyright © 2016年 duan. All rights reserved.
//

#import "JTAlertView.h"
//#import "SVProgressHUD.h"

typedef void(^AnimationCallBack)();
typedef void(^RequestCallBack)();
@interface JTAlertView ()
@property (nonatomic,copy) AnimationCallBack animationCallBack;
@property (nonatomic,copy) RequestCallBack requestCallBack;
@property (nonatomic,strong) UIAlertAction* secureTextAlertAction;
@end
@implementation JTAlertView

- (void)showSecureTextEntryAlertFromViewController:(UIViewController*)viewController withTitle:(NSString*)title1 message:(NSString*)message1 cancleTitle:(NSString*)cancleTitle okTitle:(NSString*)okTitle completionHandle:(CompletionHandle)completionHandle{
    NSString *title = title1;
    NSString *message = message1;
    NSString *cancelButtonTitle = cancleTitle;
    NSString *otherButtonTitle = okTitle;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"请输入...";
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
    }];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        // Stop listening for text changed notifications.
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        completionHandle(alertController.textFields.firstObject.text);
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];
    
    // The text field initially has no text in the text field, so we'll disable it.
    otherAction.enabled = NO;
    
    // Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
    self.secureTextAlertAction = otherAction;
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [viewController presentViewController:alertController animated:YES completion:nil];
}

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {
    UITextField *textField = notification.object;
    self.secureTextAlertAction.enabled = textField.text.length <= 15 && textField.text.length > 0;
}




- (void)showAgeAlertFromViewController:(UIViewController*)viewController withTitle:(NSString*)title1 message:(NSString*)message1 cancleTitle:(NSString*)cancleTitle okTitle:(NSString*)okTitle completionHandle:(CompletionHandle)completionHandle{
    NSString *title = title1;
    NSString *message = message1;
    NSString *cancelButtonTitle = cancleTitle;
    NSString *otherButtonTitle = okTitle;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"请输入...";
        textField.keyboardType = UIKeyboardTypeNumberPad;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ageAlertHandleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
    }];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        // Stop listening for text changed notifications.
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        completionHandle(alertController.textFields.firstObject.text);
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];
    
    // The text field initially has no text in the text field, so we'll disable it.
    otherAction.enabled = NO;
    
    // Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
    self.secureTextAlertAction = otherAction;
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [viewController presentViewController:alertController animated:YES completion:nil];
}
- (void)ageAlertHandleTextFieldTextDidChangeNotification:(NSNotification *)notification {
    UITextField *textField = notification.object;
    self.secureTextAlertAction.enabled = textField.text.integerValue >0 && textField.text.integerValue <100;
}
- (void)showPleaseLoginFromviewController:(UIViewController*)viewController loginHandel:(LoginHandle)logintHandle{
    UIAlertController* loginAlert = [UIAlertController alertControllerWithTitle:@"请先登陆" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"去登陆" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        logintHandle();
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"再看看" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [loginAlert addAction:okAction];
    [loginAlert addAction:cancleAction];
    [viewController presentViewController:loginAlert animated:YES completion:^{
        
    }];
}

- (void)showShutsdownFromviewController:(UIViewController*)viewController loginHandel:(LoginHandle)logintHandle{
    UIAlertController* loginAlert = [UIAlertController alertControllerWithTitle:@"确定要退出吗？" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        logintHandle();
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"再看看" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [loginAlert addAction:okAction];
    [loginAlert addAction:cancleAction];
    [viewController presentViewController:loginAlert animated:YES completion:^{
        
    }];
}


- (void)showWordsAlertViewWithTarget:(UIViewController*)target title:(NSString*)title loginHandel:(LoginHandle)logintHandle{
    UIAlertController* loginAlert = [UIAlertController alertControllerWithTitle:title message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        logintHandle();
    }];
    [loginAlert addAction:okAction];
    [target presentViewController:loginAlert animated:YES completion:nil];
}








@end
